<?php 
class Application {
	public static function Run(){
		//初始化 [加载配置项]
		self::_init();
		
		//设置外部链接[例：返回首页，css,js..]
		self::_set_url();
		
		//自动加载
		spl_autoload_register(array(__CLASS__,"_autoLoad"));
		
		self::_create_demo();
		
		//启动APP
		self::_app_run();
	}
	
	private static function _app_run(){
		//1.获取控制器
		$c = isset($_GET[C("VAR_CONTROLLER")]) ? $_GET[C("VAR_CONTROLLER")] : "Index";
		$c .= "Controller";
		//2.获取方法
		$a = isset($_GET[C("VAR_ACTION")]) ? $_GET["VAR_ACTION"] : "Index";
		
		//3.运行
		$controller = new $c();
		$controller->$a();
	}
	
	private static function _autoLoad($className){
		//p($className);
		
		require_once APP_CONTROLLER_PATH."/".$className.'.class.php';
	}
	
	private static function _init(){
		//1.加载 框架 配置项
		C(require_once CONFIG_PATH."/config.php");
		
		//2.加载用户配置项
		$user_config_path = APP_CONFIG_PATH."/config.php";
		$user_config_content = <<<str
<?php
//用户配置
return array(
	"Version"=>"0.01"
);				
?>
str;
		//当用户配置项不存在 ，新建
		is_file($user_config_path) || file_put_contents($user_config_path,$user_config_content);
		
		C(require_once $user_config_path); 
		//设置市区
		date_default_timezone_set(C("DEFAULT_TIME_ZONE"));
		
		//session控制
		C("SESSION_AUTO_START") && session_start();
		
	}
	
	private static function _set_url(){
		 
		$path = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
		$path = str_replace("\\", "/", $path);
		
		define('__APP__',$path);
// 		p(__APP__);
		define('__ROOT__',dirname($path));
// 		p(__ROOT__);
		define('__TPL__',__ROOT__."/".APP_NAME."/Tpl");
// 		p(__TPL__);
		define('__PUBLIC__',__ROOT__."/".APP_NAME."/Tpl/Public");
// 		p(__PUBLIC__);
		
	}
	
	private static function _create_demo(){
		
		$path = APP_CONTROLLER_PATH . "/IndexController.class.php";
		
		$content = <<<str
<?php
class IndexController extends Controller {
	public function index(){
		header('Content-type:text/html;charset=utf-8');
		echo '<h2>欢迎使用 MyFrame 框架 (:!</h2>';
	}
}				
?>
str;
		is_file($path) || file_put_contents($path, $content);
	}
	 
}

?>